#include"RRQueue.hpp"

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void* productor(void* args)
{
  RRQueue<int>* rq = static_cast<RRQueue<int>*>(args);
  while(true)
  {
    int key = rand() % 100;
    rq->push(key);
    pthread_mutex_lock(&mutex);
    cout << "I am productor. I send message to RRQueue : " << key << endl;
    pthread_mutex_unlock(&mutex);
    //sleep(1);
  }
  return nullptr;
}

void* consumer(void* args)
{
  RRQueue<int>* rq = static_cast<RRQueue<int>*>(args);
  while(true)
  {
    //sleep(1);
    int key = 0;
    rq->pop(key);
    pthread_mutex_lock(&mutex);
    cout << "I am consumer. I task messgae from RRQueue : " << key << endl;
    pthread_mutex_unlock(&mutex);
  }
  return nullptr;
}

int main()
{
  RRQueue<int>* rq = new RRQueue<int>();
  srand(time(nullptr) ^ getpid());
  pthread_t c,p;
  pthread_create(&c,nullptr,consumer,rq); 
  pthread_create(&p,nullptr,productor,rq);
  
  pthread_join(c,nullptr);
  pthread_join(p,nullptr);
  return 0;
}
